/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package business;

import java.math.BigDecimal;
import model.Account;
import model.AccountManager;
import model.CustomerManager;



/**
 *
 * @author Administrator
 */
public class CreateNewAccCmd extends AbstractCommand {

    @Override
    public boolean execute(Object args) {
        //if didn't specify an account id, generate one
        if(account.getId() == null || account.getId().equals("")) {
            int randomAccID = (int) (Math.random()*899999999+1000000000);
            account.setId(String.valueOf(randomAccID));
            while( AccountManager.getInstance().getAccountByID(account.getId()) != null ) {
                randomAccID = (int) (Math.random()*899999999+1000000000);
                account.setId(String.valueOf(randomAccID));
            }        
        }
        
        if( account==null || AccountManager.getInstance().getAccountByID(account.getId()) != null )
            return false;
        if( account.getOwnerID()==null || CustomerManager.getInstance().getCustomerByID(account.getOwnerID()) == null )
            return false;
        if( account.getBalance() == null || account.getBalance().compareTo(new BigDecimal("0")) == 0 )
            return false;
        
        
        AccountManager.getInstance().addAccount(account);
        return true;
    }
    
    private Account account;

    /**
     * @return the account
     */
    public Account getAccount() {
        return account;
    }

    /**
     * @param account the account to set
     */
    public void setAccount(Account account) {
        this.account = account;
    }
    
}
